import axios from '../utils/axios';

export interface StudentDTO {
  id: number;
  name: string;
  studentId: string;
  className: string;
  email: string;
  phone: string;
  status: string;
  lastLogin: string;
  progress: number;
}

export interface StudentDetailDTO extends StudentDTO {
  courses: CourseDTO[];
  activities: ActivityDTO[];
}

export interface CourseDTO {
  name: string;
  status: string;
  description: string;
  completion: number;
}

export interface ActivityDTO {
  type: string;
  title: string;
  content: string;
  time: string;
}

/**
 * Retrieves all students.
 * @returns A promise that resolves to an array of StudentDTO objects.
 */
export const getStudents = async () => {
  const response = await axios.get('/api/supervisor/students');
  return response.data;
};

/**
 * Retrieves the details of a student.
 * @param studentId The ID of the student.
 * @returns A promise that resolves to a StudentDetailDTO object.
 */
export const getStudentDetail = async (studentId: number) => {
  const response = await axios.get(`/api/supervisor/students/${studentId}`);
  return response.data;
};

/**
 * Updates the status of a student.
 * @param studentId The ID of the student to update.
 * @param status The new status for the student.
 * @returns A promise that resolves to a StudentDTO object.
 */
export const updateStudentStatus = async (studentId: number, status: string) => {
  const response = await axios.put(`/api/supervisor/students/${studentId}/status`, { status });
  return response.data;
};

/**
 * Deletes a student.
 * @param studentId The ID of the student to delete.
 * @returns A promise that resolves to a boolean indicating whether the deletion was successful.
 */
export const deleteStudent = async (studentId: number) => {
  const response = await axios.delete(`/api/supervisor/students/${studentId}`);
  return response.status === 200;
};

/**
 * Retrieves a paginated list of students.
 * @param page The page number to retrieve.
 * @param size The number of students per page.
 * @returns A promise that resolves to an object containing the paginated student data.
 */
export const getStudentsByPage = async (page: number, size: number) => {
  const response = await axios.get(`/api/supervisor/students/page`, {
    params: { page, size }
  });
  return response.data;
};

/**
 * Retrieves a filtered list of students.
 * @param filter An object containing the filter criteria.
 * @returns A promise that resolves to an array of StudentDTO objects.
 */
export const getStudentsByFilter = async (filter: Record<string, any>) => {
  const response = await axios.get(`/api/supervisor/students/filter`, {
    params: filter
  });
  return response.data;
};

/**
 * Assigns a student to a supervisor.
 * @param studentId The ID of the student to assign.
 * @param supervisorId The ID of the supervisor to assign the student to.
 * @returns A promise that resolves to a StudentDTO object.
 */
export const assignStudentToSupervisor = async (studentId: number, supervisorId: number) => {
  const response = await axios.put(`/api/supervisor/students/${studentId}/supervisor/${supervisorId}`);
  return response.data;
};

/**
 * Retrieves the list of students for a supervisor.
 * @param supervisorId The ID of the supervisor.
 * @returns A promise that resolves to an array of StudentDTO objects.
 */
export const getSupervisorStudents = async (supervisorId: number) => {
  const response = await axios.get(`/api/supervisor/students/supervisor/${supervisorId}`);
  return response.data;
};